package com.system.dao;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.system.entity.Permission;
import com.system.entity.Role;

@Repository
public interface RoleRepository extends PagingAndSortingRepository<Role, Long>,JpaSpecificationExecutor<Role> {

	//根据角色名找角色，级联出权限
    @Query("FROM Role r left join r.permissionList WHERE r.name =:roleName")
    public Role findPermissionsByRoleName(@Param("roleName") String roleName);
    
    //根据角色id找角色，级联出权限
    @Query("select distinct r FROM Role r left join r.permissionList WHERE r.id =:roleId")
    public Role findPermissionsByRoleId(@Param("roleId") Long roleId);
    
    //根据角色id找角色，级联出权限
    @Query("select  r.permissionList FROM Role r left join r.permissionList WHERE r.id =:roleId")
    public List<Permission> findRolePermissions(@Param("roleId") Long roleId);
    
    
    @Query("FROM Role r WHERE r.name like ?1")
	public List<Role> findByName(String name, Pageable pageable);   
    
    //根据权限找角色
    @Query("select distinct r From Role r left join r.permissionList permission where permission.id=?1")
	public List<Role> findRoleByPermission(Long id);
    
    //根据用户Id找角色
    @Query("select distinct r From Role r left join r.staffList staff where staff.id=?1")
	public List<Role> findRoleByStaff(Long id);
    
    //根据用户名找角色
    @Query("select distinct r From Role r left join r.staffList staff where staff.staffNum=?1")
	public List<Role> findRoleByStaffNum(String StaffNum);

   }


